package ClientUI;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DebugGraphics;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import javax.swing.DefaultListModel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import Protocol.noticeMsgWord.NoticeMsgWord;
import Protocol.threadStatus.ThreadStatus;
import Protocol.threadStatus.ThreadStatus_R;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BbsJPanel extends javax.swing.JPanel {
	private JTabbedPane bbsTabbedPane;
	private JTextArea editArea;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JList othersList;
	private JScrollPane jScrollPane3;
	private JList topList;
	private JPanel topTabbedPanel;
	private JButton publishButton;
	private JLabel statusLabel;
	private JLabel descLabel;
	private JPanel editPanel;
	private JPanel othersPanel;
	private DefaultListModel	listModel = new DefaultListModel();
	private DefaultListModel	othersListModel = new DefaultListModel();
	private ClientGUI	clientGui;
	
	public BbsJPanel(ClientGUI   cgui) {
		super();
		this.clientGui = cgui;
		initGUI();
	}
	public void addThread(ThreadStatus   thread) {
		SwingUtilities.invokeLater(new AddThreadRunnable(thread));
	}
	public void updateAllThreadStatus(ArrayList<ThreadStatus>  tsList) {
		
	}
	private void addThreadPrivate(ThreadStatus   thread) {
		MyTextArea	tempArea = new MyTextArea(thread);
		tempArea.setVisible(true);
		//pane.setViewportView(tempArea);
		//tmp.setVisible(true);
		if(this.bbsTabbedPane.getSelectedIndex()==0) {
			listModel.add(listModel.getSize(), tempArea);
			this.topList.repaint();
		}
		else if(this.bbsTabbedPane.getSelectedIndex() == 1) {
			othersListModel.add(othersListModel.getSize(), tempArea);
			this.othersList.repaint();
		}
		
		//this.topPanel.repaint();
		this.bbsTabbedPane.repaint();
		this.repaint();
		this.clientGui.mainFrame.repaint();
	}
	class AddThreadRunnable implements Runnable {
		ThreadStatus   thread;
		AddThreadRunnable(ThreadStatus   thread) {
			this.thread = thread;
		}
		public void run() {
			addThreadPrivate(thread) ;
		}
	}
	class MyTextArea extends JTextArea {
		ThreadStatus   thread;
		public MyTextArea(ThreadStatus   thread) {
			this.thread = thread;
			//this.setSize(topPanel.getWidth()-20, topPanel.getHeight()/4);
			this.setText("content: "+thread.getContent());
			this.setEditable(false);
			this.setVisible(true);
		}	
		public String toString() {
			return this.thread.getContent();
		}
	}
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(420, 440);
			this.setLayout(null);
			{
				bbsTabbedPane = new JTabbedPane();
				this.add(bbsTabbedPane);
				bbsTabbedPane.setBounds(22, 17, 376, 262);
				bbsTabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
				{
					topTabbedPanel = new JPanel();
					bbsTabbedPane.addTab("Top", null, topTabbedPanel, null);
					topTabbedPanel.setPreferredSize(new java.awt.Dimension(368, 154));
					topTabbedPanel.setLayout(null);
					{
						jScrollPane2 = new JScrollPane();
						topTabbedPanel.add(jScrollPane2);
						jScrollPane2.setBounds(32, 29, 309, 185);
						{
							/*ListModel topListModel = 
								new DefaultComboBoxModel(
										new String[] { "Item One", "Item Two" });*/
							topList = new JList();
							
							jScrollPane2.setViewportView(topList);
							topList.setModel(listModel);
							topList.setBounds(103, 38, 148, 61);
						}
					}
				}
				//bbsTabbedPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
				{
					othersPanel = new JPanel();
					bbsTabbedPane.addTab("Others", null, othersPanel, null);
					othersPanel.setLayout(null);
					othersPanel.setPreferredSize(new java.awt.Dimension(371, 230));
					{
						jScrollPane3 = new JScrollPane();
						othersPanel.add(jScrollPane3);
						jScrollPane3.setBounds(32, 19, 309, 192);
						{
							othersList = new JList();
							jScrollPane3.setViewportView(othersList);
							othersList.setModel(othersListModel);
							othersList.setBounds(103, 38, 148, 61);
						}
					}
				}
			}
			{
				editPanel = new JPanel();
				this.add(editPanel);
				editPanel.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				editPanel.setLayout(null);
				editPanel.setBounds(22, 291, 376, 137);
				{
					descLabel = new JLabel();
					editPanel.add(descLabel);
					descLabel.setText("input your announcement here");
					descLabel.setBounds(14, 16, 198, 17);
				}
				{
					statusLabel = new JLabel();
					editPanel.add(statusLabel);
					statusLabel.setText("failed !");
					statusLabel.setHorizontalTextPosition(SwingConstants.CENTER);
					statusLabel.setVisible(false);
					statusLabel.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
					statusLabel.setBounds(14, 129, 227, 18);
				}
				{
					publishButton = new JButton();
					editPanel.add(publishButton);
					publishButton.setText("Publish");
					publishButton.setBounds(271, 107, 76, 24);
					publishButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							handlePublishButton();
						}
					});
				}
				{
					jScrollPane1 = new JScrollPane();
					editPanel.add(jScrollPane1);
					jScrollPane1.setOpaque(false);
					jScrollPane1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
					jScrollPane1.setBounds(14, 39, 333, 62);
					{
						editArea = new JTextArea();
						jScrollPane1.setViewportView(editArea);
						editArea.setText("your thread here");
						editArea.setPreferredSize(new java.awt.Dimension(312, 50));
						editArea.setBounds(12, 55, 344, 101);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void handlePublishButton() {
		// TODO Auto-generated method stub
		ThreadStatus   notice;
		notice = new ThreadStatus();
		ThreadStatus_R  nr= new ThreadStatus_R();
		
		notice.setContent(this.editArea.getText());
		notice.setType("thread");
		notice.setAuthor_id(clientGui.getClient().getClientData().getSelf_basic().getUser_id());
		//notice.setTitle(this.tittleField.getText());	
		nr.setThreadLogStatus(notice);
		nr.setOperationType("publish");
	//	nr.setUser_id(notice.getFrom_id());
		
		clientGui.getClient().getClientNet().sendMsgTo(clientGui.getClient().getClientData().getServer_basic(), nr);
		
		addThread(notice);
		
	}

}
